from __future__ import absolute_import

import datetime

from celery import shared_task, task
from celery.utils.log import get_task_logger

from .models import LabLog


@shared_task()
def auto_add_lablog():
    today = datetime.date.today()
    yesterday = today - datetime.timedelta(days=1)

    try:
        obj = LabLog.objects.get(time_slot=1, log_date=yesterday)
    except LabLog.DoesNotExist:
        log_time = datetime.time(hour=9, minute=0, second=0)
        LabLog.objects.create(time_slot=1, log_date=yesterday, log_time=log_time, adding_way=1)

    try:
        obj = LabLog.objects.get(time_slot=2, log_date=yesterday)
    except LabLog.DoesNotExist:
        log_time = datetime.time(hour=21, minute=0, second=0)
        LabLog.objects.create(time_slot=2, log_date=yesterday, log_time=log_time, adding_way=1)


@shared_task()
def add(x, y):
    print(x + y)
    return x + y


@shared_task()
def mul(x, y):
    print("%d * %d = %d" % (x, y, x * y))
    return x * y


@shared_task()
def sub(x, y):
    print("%d - %d = %d" % (x, y, x - y))
    return x - y


@task(ignore_result=True, max_retries=1, default_retry_delay=10)
def just_print():
    print("Print from celery task")
